

################
#FIGURE 1
################

bp1 <- ggplot(thiscong, aes(factor(vote_against_boehner),ideals)) +
  geom_violin() +
  geom_point() +
  xlab("") +
  ylab("Ideal Point") +
  scale_x_discrete(breaks=seq(0,1), labels = c("For Boehner","Against Boehner")) +
  coord_flip() +
  theme_bw()


bp2 <- ggplot(thiscong, aes(factor(vote_against_boehner),party_unity)) +
  geom_violin() +
  geom_point() +
  ylab("Party Unity Scores") +
  xlab("") +
  scale_x_discrete(breaks=seq(0,1), labels = c("For Boehner","Against Boehner")) +
  coord_flip() +
  theme_bw()

##diplay the plot

ggsave(bp1, file = paste0(getwd(), "/Figure1a.pdf"))
ggsave(bp2, file = paste0(getwd(), "/Figure1b.pdf"))

################
#FIGURE 2
################

#make plots of vote choices
N <- nrow(thiscong)
plotting_rebellion <- data.frame(vote_type=c(rep("Boehner Removal",3*N),
                                             rep("Freedom Caucus Membership",3*N)),
                                 loyalty_measure=c(rep("Unity Scores",N),rep("MV Loyalty",N),rep("IRT Loyalty",N),
                                                   rep("Unity Scores",N),rep("MV Loyalty",N),rep("IRT Loyalty",N)),
                                 loyalty_score=c(thiscong$party_unity,thiscong$responsiveness_to_party_calls,thiscong$loyalty,
                                                 thiscong$party_unity,thiscong$responsiveness_to_party_calls,thiscong$loyalty),
                                 decision=c(rep(thiscong$vote_against_boehner,3),rep(thiscong$freedom_caucus,3)))

plotting_rebellion$plotpars <- NA
plotting_rebellion$plotpars[plotting_rebellion$decision==1 & plotting_rebellion$vote_type=="Boehner Removal"] <- "Boehner Vote - Remove"
plotting_rebellion$plotpars[plotting_rebellion$decision==0 & plotting_rebellion$vote_type=="Boehner Removal"] <- "Boehner Vote - Keep"
plotting_rebellion$plotpars[plotting_rebellion$decision==1 & plotting_rebellion$vote_type=="Freedom Caucus Membership"] <- "Freedom Caucus - Member"
plotting_rebellion$plotpars[plotting_rebellion$decision==0 & plotting_rebellion$vote_type=="Freedom Caucus Membership"] <- "Freedom Caucus - Non-Member"



loyalty_rebellion_plot <-
  ggplot(plotting_rebellion,
         aes(loyalty_score,
             colour=factor(plotpars),
             fill=factor(plotpars))) +
  geom_density(alpha=0.3) +
  scale_color_manual(name="", values=c("purple","red","red","purple")) +
  scale_fill_manual(name="", values=c("purple","red","red","purple")) +
  facet_grid(loyalty_measure ~ vote_type, scales="free") +
  xlab("Loyalty Measure") +
  ylab("Kernel Density") +
  theme_bw() +
  theme(legend.position = "bottom")

##display the plot



ggsave(loyalty_rebellion_plot, file = paste0(getwd(), "/Figure2.pdf"),height=8, width=10)


################
#FIGURE 3
################

gg_comp <- 
  ggpairs(as.data.frame(cbind("Party Unity"=all_together5$party_unity, 
                              "MV Loyalty"= all_together5$responsiveness_to_party_calls, 
                              "IRT Loyalty"=all_together5$loyalty, 
                              "Extremism"=abs(all_together5$ideals)))) + 
  theme_bw()

##display the plot



ggsave(gg_comp, file = paste0(getwd(), "/Figure3.pdf"), height = 10, width=10)


################
#FIGURE 4
################


rank_plot_over_time <-
  ggplot(all_together, aes(ideals,loyaltyRank-unityRank, colour=factor(party_code))) +
  geom_point(size=3) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  scale_color_manual(name="Party", values=c("blue","red"), labels=c("Democrats","Republicans")) +
  scale_fill_manual(name="Party", values=c("blue","red"), labels=c("Democrats","Republicans")) +
  ylab("IRT Loyalty Rank Minus Party Unity Rank (within-party)") +
  xlab("Ideal Point") +
  facet_wrap(~congress, scales="fixed") +
  theme_bw()

##display the plot

ggsave(rank_plot_over_time, file = paste0(getwd(), "/Figure4.pdf"), height = 10, width=10)



################
#FIGURE 5
################

party_trend <-
  all_together %>% 
  group_by(congress, party_code) %>% 
  dplyr::summarise(m = mean(loyalty)) %>%
  ungroup() %>% 
  ggplot(aes(congress, m,colour = factor(party_code))) + 
  geom_point() + 
  geom_line() +
  geom_vline(xintercept = c(103.5, 109.5, 111.5), linetype = "dashed", size=1) +
  ylab("Average Within-Party IRT Loyalty") +
  xlab("Congress") +
  scale_x_continuous(breaks=101:113, limits=c(101,113)) +
  scale_color_manual(name="Party", values=c("blue","red"), labels=c("Democrats","Republicans")) +
  annotate("text", x = c(102, 110.5), y =45, label = "Dem Majority", size = 3) + 
  annotate("text", x = c(106, 112.5), y =45, label = "GOP Majority", size = 3) + 
  theme_bw() + 
  theme(legend.position = "bottom")

##display the plot


ggsave(party_trend, file = paste0(getwd(), "/Figure5.pdf"), height=8, width=10)



################
#FIGURE 6
################

irt_maj_status <-
  all_together %>%
  mutate(Majority = case_when(MajorityParty == 1 ~ "Majority",
                              TRUE ~ "Minority"),
         Party = case_when(party_code == 200 ~ "Republican",
                           TRUE ~ "Democrat")) %>%
  ggplot(aes(ideals,loyalty)) + 
  geom_point() + 
  stat_smooth() + 
  facet_grid(Majority ~ Party, scales = "free") +
  ylab("IRT Loyalty") +
  xlab("Ideal Point") +
  theme_bw()

##display the plot

ggsave(irt_maj_status, file = paste0(getwd(), "/Figure6.pdf"), height=8, width=10)



################
#FIGURE 7
################

member_plot <-
  ggplot(all_together %>% 
           filter(icpsr %in% c(14863,14290,29106,15033)), 
         aes(congress, loyalty, colour = bioname, fill = bioname)) + 
  geom_point(size=2) +
  geom_line(size=1.25) +
  geom_ribbon(aes(ymin = loyalty_low, ymax = loyalty_high), alpha = 0.2) +
  geom_vline(xintercept = c(103.5, 109.5, 111.5), linetype = "dashed", size=1) +
  geom_hline(yintercept = 50, linetype = "dotted") +
  ylab("IRT Loyalty") +
  xlab("Congress") +
  scale_x_continuous(breaks=101:113) +
  annotate("text", x = c(102, 110.5), y =0.9, label = "Dem Majority", size = 3) + 
  annotate("text", x = c(106, 112.5), y =0.9, label = "GOP Majority", size = 3) + 
  facet_wrap(~ bioname, nrow = 2) +
  theme_bw() + 
  theme(legend.position = "none")

##display the plot

ggsave(member_plot, file = paste0(getwd(), "/Figure7.pdf"), width = 12, height = 10)

cat("Replication file 6 done.","\n")
